﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace timetableasy.Models
{
    public class UserRepository
    {
        private TimeTableDBDataContext db = new TimeTableDBDataContext();

        //Query Methods
        public IQueryable<User> FindAllUsers()
        {
            return db.Users;
        }

        public User GetUserById(int id)
        {
            return db.Users.SingleOrDefault(u => u.UserId == id);
        }

        public int GetUserIdByName(string name)
        {
            return db.Users.SingleOrDefault(u => u.Login == name).UserId;
        }

        //Insert/Delete Methods

        public void Add(User user)
        {
            db.Users.InsertOnSubmit(user);
        }

        public int Add2(User user)
        {
            Add(user);
            return GetUserIdByName(user.Login);
        }

        public void Delete(User user)
        {
            db.Users.DeleteOnSubmit(user);
        }

        //Persistence

        public void Save()
        {
            db.SubmitChanges();
        }
    }
}